//
//  TabBBarViewController.m
//  JujiubangBuiness
//
//  Created by xiaodigua on 2022/9/26.
//

#import "TabBarController.h"
@interface TabBarController ()<UITabBarControllerDelegate>
@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.backgroundImage = [[UIImage alloc]init];
    self.tabBar.translucent = NO;
    self.delegate = self;
    self.tabBar.layer.shadowColor = MYRed.CGColor;
    self.tabBar.layer.shadowOffset = CGSizeMake(0, 1);
    self.tabBar.layer.shadowOpacity = 0.4;
    self.tabBar.layer.shadowRadius = 3;
    if (@available(iOS 15.0, *)) {

    UITabBarAppearance *bar = [UITabBarAppearance new];

    bar.backgroundEffect = nil;

    bar.stackedLayoutAppearance.selected.titleTextAttributes = @{NSFontAttributeName:MY_SCALE_BoldFont(11),NSForegroundColorAttributeName:MYOrange};

    bar.stackedLayoutAppearance.normal.titleTextAttributes = @{NSFontAttributeName:MY_SCALE_BoldFont(11),NSForegroundColorAttributeName:MyColor(@"999999")};
    bar.stackedLayoutAppearance.normal.iconColor = UIColor.whiteColor;
    bar.stackedLayoutAppearance.selected.iconColor = MYOrange;
        
    self.tabBar.scrollEdgeAppearance = bar;
    self.tabBar.standardAppearance = bar;

    } else {

    self.tabBar.backgroundColor = [UIColor whiteColor];

    self.tabBar.barTintColor = [UIColor whiteColor];

    self.tabBar.translucent = NO;

//    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]]];

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor.whiteColor} forState:UIControlStateNormal];

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];

    }
    [self addChildVCs];
}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//    LNavigationViewController *vc = (LNavigationViewController *)viewController;
    return YES;
}
// Do any additional setup after loading the view.
- (void)dealloc {
    NSLog(@"%@ dealloc", NSStringFromClass([self class]));
}

- (void)addChildVCs {
    [self addChildVC:[[HomeController alloc] init] title:LocalizedString(@"首页") imageName:@"bar_1"];
    [self addChildVC:[[ToolsController alloc] init] title:LocalizedString(@"工具") imageName:@"bar_2"];
    [self addChildVC:[[MeController alloc] init] title:LocalizedString(@"我的") imageName:@"bar_3"];
}

- (void)addChildVC:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName {
    vc.tabBarItem.title = title;
    UIImage *normalImg = [Helper imageWithColor:MyColor(@"999999") withImage:[UIImage imageNamed:imageName]];
    UIImage *selectImg = [Helper imageWithColor:MYOrange withImage:[UIImage imageNamed:imageName]];
    vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[normalImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] tag:0];
    vc.tabBarItem.selectedImage = [selectImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    if (@available(iOS 13.0, *)) {
        [self.tabBar.standardAppearance qmui_applyItemAppearanceWithBlock:^(UITabBarItemAppearance * _Nonnull itemAppearance) {
            itemAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, 0);
        }];
    } else {
        vc.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 0);
    }
    LNavigationViewController *nav = [LNavigationViewController rootVC:vc];
    nav.gk_openScrollLeftPush = YES;
    nav.gk_openSystemNavHandle = YES;
    [self addChildViewController:nav];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
//    if (self.tabBar.items.count > 2) {
//        UITabBarItem *item = self.tabBar.items[2];
//        item.imageInsets = UIEdgeInsetsMake(-25, 0, 0, 0);
//    }
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
//    NSLog(@"监听的是item文字: %@", item.title);
//    NSInteger index = [self.tabBar.items indexOfObject:item];
//    if (index != self.indexFlag) {
//        NSInteger index = [self.tabBar.items indexOfObject:item];
//           if (index != self.indexFlag) {
//               //执行动画
//               NSMutableArray *arry = [NSMutableArray array];
//               for (UIView *btn in self.tabBar.subviews) {
//                   if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
//                        [arry addObject:btn];
//                   }
//               }
//               //添加动画
//               //放大效果，并回到原位
//               CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//               //速度控制函数，控制动画运行的节奏
//               animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//               animation.duration = 0.2;       //执行时间
//               animation.repeatCount = 1;      //执行次数
//               animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
//               animation.fromValue = [NSNumber numberWithFloat:0.9];   //初始伸缩倍数
//               animation.toValue = [NSNumber numberWithFloat:1.1];     //结束伸缩倍数
//               UIView *curren = (UIView *)arry[index];
//               [[curren layer] addAnimation:animation forKey:nil];
//
//               self.indexFlag = index;
//           }
//    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
